package common.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DatabaseManagerImpl implements DatabaseManager {
	
	private BasicDataSource dataSource;
	
	public DatabaseManagerImpl(BasicDataSource dataSource) {
		this.dataSource = dataSource;
	}

	
	@Override
	public Connection getConnection() throws SQLException{
		return dataSource.getConnection();
	}
	
	
	@Override
	public void close(Connection conn, PreparedStatement pstmt){
		
		if(pstmt != null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void close(Connection conn, PreparedStatement pstmt, ResultSet rset){
		if(rset != null){
			try {
				rset.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(pstmt != null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
















